package com.sengled.cloud.biz.union.mgt.dao.sys.provider;

import com.sengled.cloud.biz.union.mgt.common.PageUtil;
import com.sengled.cloud.biz.union.mgt.model.po.Role;
import com.sengled.cloud.biz.union.mgt.model.po.ShopAccount;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
 * Created by csolo on 2017/11/8.
 */
public class ShopAccountDaoProvider {

    public String updateShopAccountSQL(ShopAccount shopAccount) {
        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE shop_account set ");
        StringBuilder setStr = new StringBuilder();
        if(shopAccount.getAccountCode() != null){
            setStr.append(" account_code = #{accountCode}, ");
        }
        if(shopAccount.getAccountPwd() != null){
            setStr.append(" account_pwd = #{accountPwd}, ");
        }
        if(shopAccount.getAccountStatus() != null){
            setStr.append(" account_status = #{accountStatus}, ");
        }
        if(shopAccount.getShopId() != null){
            setStr.append(" shop_id = #{shopId}, ");
        }
        if(shopAccount.getShopCode() != null){
            setStr.append(" shop_code = #{shopCode}, ");
        }
        if(shopAccount.getShopName() != null){
            setStr.append(" shop_name = #{shopName}, ");
        }
        if(shopAccount.getQrCode() != null){
            setStr.append(" qr_code = #{qrCode}, ");
        }
        if(shopAccount.getCompanyId() != null){
            setStr.append(" company_id = #{companyId}, ");
        }
        if(shopAccount.getCreateDate() != null){
            setStr.append(" create_date = #{createDate}, ");
        }
        if(shopAccount.getOperatorId() != null){
            setStr.append(" operator_id = #{operatorId}, ");
        }
        if(shopAccount.getSealMark() != null){
            setStr.append(" seal_mark = #{sealMark}, ");
        }
        if(shopAccount.getEnableMark() != null){
            setStr.append(" enable_mark = #{enableMark}, ");
        }
        setStr.deleteCharAt(setStr.length() - 2);
        sb.append(setStr);
        sb.append(" where id = #{id} ");
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

    public String queryShopAccountSQL(Map map) {
        ShopAccount shopAccount = (ShopAccount)map.get("shopAccount");
        PageUtil pageUtil = (PageUtil)map.get("pageUtil");

        StringBuilder sb = new StringBuilder();
        sb.append("select id as id,account_code as accountCode,account_pwd as accountPwd,account_status as accountStatus" +
                ",company_id as companyId,shop_id as shopId,shop_code as shopCode,shop_name as shopName,qr_code as qrCode,create_date as createDate,operator_id as operatorId,seal_mark as sealMark" +
                ",enable_mark as enableMark from shop_account where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
        if(shopAccount.getId() != null){
            setStr.append(" and id = #{shopAccount.id} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getAccountCode())){
            setStr.append(" and account_code = #{shopAccount.accountCode} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getAccountPwd())){
            setStr.append(" and account_pwd = #{shopAccount.accountPwd} ");
        }
        if(shopAccount.getAccountStatus() != null){
            setStr.append(" and account_status = #{shopAccount.accountStatus} ");
        }
        if(shopAccount.getCompanyId() != null){
            setStr.append(" and company_id = #{shopAccount.companyId} ");
        }
        if(shopAccount.getShopId() != null){
            setStr.append(" and shop_id = #{shopAccount.shopId} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getShopCode())){
            setStr.append(" and shop_code like CONCAT('%', #{shopAccount.shopCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getShopName())){
            setStr.append(" and shop_name like CONCAT('%', #{shopAccount.shopName}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getQrCode())){
            setStr.append(" and qr_code = #{shopAccount.qrCode} ");
        }
        if(shopAccount.getCreateDate() != null){
            setStr.append(" and create_date = #{shopAccount.createDate} ");
        }
        if(shopAccount.getOperatorId() != null){
            setStr.append(" and operator_id = #{shopAccount.operatorId} ");
        }
        /*if(role.getSealMark() != null){
            setStr.append(" and seal_mark = #{role.sealMark} ");
        }*/
        if(shopAccount.getEnableMark() != null){
            setStr.append(" and enable_mark = #{shopAccount.enableMark} ");
        }
        if(pageUtil != null){
            setStr.append(" order by create_date desc limit #{pageUtil.start},#{pageUtil.pageSize}");
        }
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

    public String queryShopAccountCountSQL(Map map) {
        ShopAccount shopAccount = (ShopAccount)map.get("shopAccount");

        StringBuilder sb = new StringBuilder();
        sb.append("select count(id) from shop_account where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
        if(shopAccount.getId() != null){
            setStr.append(" and id = #{shopAccount.id} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getAccountCode())){
            setStr.append(" and account_code = #{shopAccount.accountCode} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getAccountPwd())){
            setStr.append(" and account_pwd = #{shopAccount.accountPwd} ");
        }
        if(shopAccount.getAccountStatus() != null){
            setStr.append(" and account_status = #{shopAccount.accountStatus} ");
        }
        if(shopAccount.getCompanyId() != null){
            setStr.append(" and company_id = #{shopAccount.companyId} ");
        }
        if(shopAccount.getShopId() != null){
            setStr.append(" and shop_id = #{shopAccount.shopId} ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getShopCode())){
            setStr.append(" and shop_code like CONCAT('%', #{shopAccount.shopCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getShopName())){
            setStr.append(" and shop_name like CONCAT('%', #{shopAccount.shopName}, '%') ");
        }
        if(StringUtils.isNotEmpty(shopAccount.getQrCode())){
            setStr.append(" and qr_code = #{shopAccount.qrCode} ");
        }
        if(shopAccount.getCreateDate() != null){
            setStr.append(" and create_date = #{shopAccount.createDate} ");
        }
        if(shopAccount.getOperatorId() != null){
            setStr.append(" and operator_id = #{shopAccount.operatorId} ");
        }
        /*if(role.getSealMark() != null){
            setStr.append(" and seal_mark = #{role.sealMark} ");
        }*/
        if(shopAccount.getEnableMark() != null){
            setStr.append(" and enable_mark = #{shopAccount.enableMark} ");
        }
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

}
